package com.gl.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gl.order.commom.ResultBean;
import com.gl.order.entity.TUser;
import com.gl.order.entity.dto.EditPwdDto;
import com.gl.order.entity.dto.UpdateUserDto;
import com.gl.order.entity.dto.UserAddDto;
import com.gl.order.entity.dto.UserPageDto;
import com.gl.order.entity.vo.UserAndRoleCodeVo;
import com.gl.order.entity.vo.UserPageVo;
import com.gl.order.security.model.SecurityUser;
import com.gl.order.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author lsy
 * @Date 2021/5/19 16:57
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/")
    @ApiOperation("获取用户信息列表")
    public ResultBean<List<TUser>> getUserList(){
        List<TUser> users = userService.list();
        return ResultBean.success(users);
    }

    @GetMapping("/{id}")
    @ApiOperation("获取用户id获取用户信息")
    @ApiImplicitParam(name = "id", value = "用户id", dataType = "String", paramType = "path")
    public ResultBean<TUser> getUserById(@PathVariable("id") String id){
        TUser user = userService.findById(id);
        return ResultBean.success(user);
    }

    @GetMapping("/current")
    @ApiOperation("获取当前登陆用户的信息")
    public ResultBean<SecurityUser> getInfo(){
        SecurityUser principal = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return  ResultBean.success(principal);
    }

    @PostMapping("/page")
    @ApiOperation("获取用户信息，分页查询")
    public ResultBean<IPage<UserPageVo>>  pageOfUser(@RequestBody @Valid UserPageDto dto){
        IPage<UserPageVo> users = userService.pageOfUser(dto);
        return ResultBean.success(users);
    }


    @PostMapping("/add")
    @ApiOperation("添加用户")
    public ResultBean addUser(@RequestBody @Valid UserAddDto dto){
        TUser user = userService.findUserByAccount(dto.getAccount());
        if(!Objects.isNull(user)){
            return ResultBean.failed("账号已存在");
        }
        userService.add(dto);
        return ResultBean.success();
    }

    @PostMapping("/edit")
    @ApiOperation("更新用户信息")
    public ResultBean updateUser(@RequestBody @Valid UpdateUserDto dto){
        userService.update(dto);
        return ResultBean.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "id",value = "用户id" , dataType = "String", paramType = "path")
    public ResultBean deleteUser(@PathVariable("id") String id){
        userService.delUser(id);
        return ResultBean.success();
    }

    @PostMapping("/del")
    @ApiOperation("批量删除用户")
    @ApiImplicitParam(name = "ids",value = "用户id数组" , dataType = "List", paramType = "body")
    public ResultBean deleteUsers(@RequestBody List<String> ids){
        userService.delByids(ids);
        return ResultBean.success();
    }

    @PostMapping("/editPwd")
    @ApiOperation("修改密码")
    public ResultBean editPwd(@RequestBody EditPwdDto dto){
        userService.editPassword(dto);
        return ResultBean.success();
    }
}
